package com.nttuan.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;

import com.nttuan.model.entity.Contact;
import com.nttuan.model.entity.User;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by TuanNT on 12/3/2014.
 */
public class ContactSupport {
    public static List<User> getContacts(Context context) {
        ContentResolver cr = context.getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        List<User> users = new ArrayList<User>();
        User user;
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                user = new User();
                user.setUserName(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))); //set display name
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//                    phone number
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                    while (pCur.moveToNext()) {
                        user.setPhone(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                        pCur.close();
                        break;
                    }
                }
//                 email
                Cursor emailCur = cr.query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { id }, null);
                while (emailCur.moveToNext()) {
                   user.setEmail(emailCur.getString(emailCur .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
                   emailCur.close();
                   break;
                }
//                 address
                String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
                String[] addrWhereParams = new String[]{id,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
                Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI,null, addrWhere, addrWhereParams, null);
                while(addrCur.moveToNext()) {
                    StringBuilder sb = new StringBuilder();
                    String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                    if(street != null)
                        sb.append(street);
                    sb.append(" ");
                    String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                    if(city != null)
                        sb.append(city);
                    user.setAddress(sb.toString());
                    break;
                }
            user.setActive(true);
            users.add(user);
            }//end while
        }
        return users;
    }
}
